package com.java.comprehensive.arithmetic.leecode.tags.hashmap;

import java.util.*;

/**
 * @Author: Dgg
 * @Date: 2021/12/25 21:32
 */
public class LeetCode_496 {

	public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
		int[] result = new int[nums1.length];
		Map map = new HashMap();
		Stack<Integer> stack = new Stack<>();
		// List<Integer> stack = new ArrayList<>();
		for (int i = nums2.length - 1; i >= 0; i--) {
			int nums = nums2[i];
			while (!stack.empty() && stack.peek() <= nums) {
				stack.pop();
			}
			map.put(nums, stack.isEmpty() ? -1 : stack.pop());
			stack.push(nums);
		}

		for (int i = 0; i < nums1.length; i++) {
			int num = nums1[i];
			result[i] = (int) map.get(num);
		}

		return result;
	}

	public static void main(String[] args) {
		int[] nums1 = { 4, 2, 1 }, nums2 = { 1, 3, 4, 2 };
		int[] ints = nextGreaterElement(nums1, nums2);
		System.out.println(Arrays.toString(ints));
	}
}
